<template>
  <div class="login">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <h1 class="title">欢迎登录</h1>
      <!-- <el-form-item>
        <el-input
          v-model="wsUrl"
          type="text"
          placeholder="请输入webscoket服务器地址"
        >
        </el-input>
      </el-form-item> -->
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="请输入账号"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="请输入密码"
        >
        </el-input>
      </el-form-item>
      <div class="code">
        <el-form-item prop="code" class="code">
          <el-input
            v-model="loginForm.code"
            type="text"
            auto-complete="off"
            placeholder="请输入验证码"
            @keyup.enter.native="submitForm()"
          >
          </el-input>
        </el-form-item>
        <img :src="imgSrc" @click="refresh" />
      </div>
      <div class="option">
        <router-link to="forgetPassword">忘记密码</router-link>
        <router-link to="register">申请注册</router-link>
      </div>
      <el-button
        :loading="loading"
        size="medium"
        type="primary"
        style="width: 100%"
        @click="submitForm()"
      >
        <span v-if="!loading">登 录</span>
        <span v-else>登 录 中...</span>
      </el-button>
    </el-form>
  </div>
</template>

<script>
import { getCodeImg, login } from "@/api/login";
import { getUserInfo } from "@/api/user";
import { setToken } from "@/utils/auth";

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
        code: "",
        uuid: "",
      },
      wsUrl: "ws://192.168.1.4:8080/websocket/",
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [
          { required: true, trigger: "blur", message: "请输入您的验证码" },
        ],
      },
      imgSrc: "",
      loading: false,
    };
  },
  methods: {
    submitForm() {
      this.$refs["loginForm"].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.login();
          this.loading = false;
        } else {
          return false;
        }
      });
    },
    async getImg() {
      await getCodeImg().then((res) => {
        this.imgSrc = res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    refresh() {
      this.getImg();
    },
    login() {
      login(this.loginForm)
        .then((res) => {
          getUserInfo().then((res) => {
            if (res.data.enterAssess === 1) {
              this.$router.push("/personal");
            } else {
              this.$router.push("/test/user");
            }
          });
          // const wsUrl = this.wsUrl + this.loginForm.username;
          // localStorage.setItem("wsUrl", wsUrl);
          // this.$store.dispatch("initWebSocket", wsUrl);
          localStorage.setItem("username", this.loginForm.username);
          setToken(res.token);
          this.$modal.msgSuccess("登录成功！", wsUrl);
        })
        .catch((err) => {
          this.getImg();
        });
    },
  },
  created() {
    this.getImg();
  },
};
</script>

<style lang="scss" scoped>
.login {
  padding-right: 7%;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  width: 100%;
  height: 100%;
  background-color: rgba(245, 248, 255, 0.7);
  background-image: url("../assets/images/bg.png");
  background-size: 100% 100%;
}

.login-form {
  border-radius: 24px;
  background: rgba($color: #fff, $alpha: 0.6);
  width: 616px;
  padding: 70px 64px;

  .title {
    margin: 0px auto 30px auto;
    font-size: 48px;
    color: #06080c;
  }

  .option {
    margin: 2px 0 64px;
    width: 488px;
    height: 20px;
    line-height: 20px;
    display: flex;
    justify-content: space-between;
    font-size: 20px;
    color: #666;
  }

  .el-form-item {
    margin-bottom: 32px;

    .el-input {
      height: 72px;
      border-radius: 8px;

      &::v-deep input {
        padding-left: 36px;
        height: 72px;
        font-size: 24px;
      }
    }

    &::v-deep .el-form-item__error {
      font-size: 16px;
    }
  }

  .code {
    display: flex;
    justify-content: space-between;

    .el-form-item__content {
      .el-input {
        width: 296px;
        height: 72px;

        &::v-deep input {
          width: 100%;
        }
      }
    }

    img {
      width: 168px;
      height: 72px;
      border-radius: 8px;
    }
  }

  .el-button {
    width: 488px;
    height: 72px;
    font-size: 30px;
    border-radius: 36px;
  }
}
</style>
